package plantshop.model;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;

import plantshop.controller.Controller;
import plantshop.view.View;



public class Player implements Serializable {
	private static final long serialVersionUID = 1880615284904591649L;
	final static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	final static int frameWidth = screenSize.width;
   	final static int frameHeight = screenSize.height;
   	
	protected String name;
	protected boolean gender;  //false = female, true = male
	private int score, money;
	private List<Opponent> opponents;
	private Game game;
	private transient View view;
	private transient Controller controller;
	
	public Player() {
		score = 0;
	}
	
	public Player(Player p, Map<String, BufferedImage> images, JFrame frame, Map<String, BufferedImage[]> animations){
		name = p.name;
		gender = p.gender;
		game = p.game;
		CollisionDetector.setStore(game.getStore());
		view = new View(game, this, images, frame, false, animations, true);
		controller = new Controller(game,view);
		opponents = p.getOpponents();
		updateTotalScore();
		frame.getContentPane().add(view);
		frame.setVisible(true);
	}
	
	public Player(String name, boolean gender, Map<String, BufferedImage> images, JFrame frame, Map<String, BufferedImage[]> animations){
		score = -1;
		money = -1;
		this.name = name;
		this.gender = gender;
		game = new Game(frameWidth/1600.0, frameHeight/900.0);
		view = new View(game, this, images, frame, false, animations, false);
		controller = new Controller(game,view);
		opponents = new ArrayList<Opponent>();
		updateTotalScore();
		frame.getContentPane().add(view);
		frame.setVisible(true);

	}
	
	public boolean getGender(){
		return gender;
	}
	
	public String getName(){
		return name;
	}
	
	public int getMoney(){
		if(money >= 0)
			return money;
		return game.getStore().getMoney();
	}
	
	public void setGender(boolean gender){
		this.gender = gender;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public void setScore(int score){
		this.score = score;
	}
	
	public void setMoney(int money){
		this.money = money;
	}
	
	public void updateTotalScore(){
		int num = getStoreAttractiveness();
		for(Opponent o: opponents){
			num += o.getRating();
		}
		game.updateTotalScore(num);
	}
	
	public int getStoreAttractiveness(){
		if(score >= 0)
			return score;
		return game.getStoreAttractiveness();
	}
	
	public void addOpponent(boolean gender, String name){
		opponents.add(new Opponent(gender, name));
		game.addOpponent();
	}
	
	public List<Opponent> getOpponents(){
		return opponents;
	}
	
	public void startGame(){
		
		game.startGame();
		controller.start();
	}
}
